package com.whlilong.jxc.ui.sorter;


import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;

import com.whlilong.jxc.model.Goodsbrand;
import com.whlilong.jxc.model.Hsinfo;
import com.whlilong.jxc.model.Khinfo;

public class BrandInfoSorter extends ViewerSorter {
	
	// 每列对应一个不同的常量，正数表示要升序、相反数表示要降序
	private static final int ID = 1;

	private static final int sbid = 2;

	private static final int sbname = 3;


	// 给外界使用排序器对象

	public static final BrandInfoSorter ID_ASC = new BrandInfoSorter(ID);

	public static final BrandInfoSorter ID_DESC = new BrandInfoSorter(-ID);

	public static final BrandInfoSorter sbid_asc = new BrandInfoSorter(sbid);

	public static final BrandInfoSorter sbid_desc = new BrandInfoSorter(-sbid);

	public static final BrandInfoSorter NAME_ASC = new BrandInfoSorter(sbname);

	public static final BrandInfoSorter NAME_DESC = new BrandInfoSorter(-sbname);


	// 当前所要排序的列，取自上面的ID、NAME两值或其相反数

	private int sortType;

	// 构造函数用private，表示不能在外部创建MySorter对象

	private BrandInfoSorter(int sortType) {

		this.sortType = sortType;

	}

	// 最关键的比较方法compare，改写自ViewerSorter。方法返回值是一个int值：正数则

	// obj1移到obj2之前；零则obj1和obj2的位置不动；负数则obj1移到obj2之后

	public int compare(Viewer viewer, Object obj1, Object obj2) {

		// 传入两条记录（实体类），然后依列给出它们的先后顺序

		Goodsbrand o1 = (Goodsbrand) obj1;

		Goodsbrand o2 = (Goodsbrand) obj2;

		switch (sortType) {

		case ID: {

			int l1 = o1.getId();

			int l2 = o2.getId();

			// Long的compareTo方法返回值有三个可能值1,0,-1：

			// 如l1＞l2则返回1；如l1＝l2则返回0；如l1＜l2则返回-1

			return l1 > l2 ? 1 : -1;

		}

		case -ID: {

			int l1 = o1.getId();

			int l2 = o2.getId();

			return l2 > l1 ? 1 : -1;

		}

		case sbname: {

			String s1 = o1.getGoodsbrandname();

			String s2 = o2.getGoodsbrandname();

			return s1.compareTo(s2);

		}

		case -sbname: {
			String s1 = o1.getGoodsbrandname();
			String s2 = o2.getGoodsbrandname();
			return s2.compareTo(s1);
		}
		case sbid: {
			String s1 = o1.getGoodsbrandid();

			String s2 = o2.getGoodsbrandid();

			return s1.compareTo(s2);
		}
		case -sbid: {
			String s1 = o1.getGoodsbrandid();

			String s2 = o2.getGoodsbrandid();

			return s2.compareTo(s1);
		}

		}

		return 0;

	}

}
